package com.ruoyi.web.controller.community.notice;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.community.domain.SimpleNoticePreview;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.SimpleNoticeContent;
import com.ruoyi.community.service.ISimpleNoticeContentService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公告列表Controller
 * 
 * @author ruoyi
 * @date 2021-12-10
 */
@RestController
@RequestMapping("/community/notice/abandon")
public class SimpleNoticeContentController extends BaseController
{
    @Autowired
    private ISimpleNoticeContentService simpleNoticeContentService;

    /**
     * 查询公告列表列表@PreAuthorize("@ss.hasPermi('community:content:list')")
     */
    @GetMapping("/list")
    public TableDataInfo list(SimpleNoticeContent simpleNoticeContent)
    {
        startPage();
        List<SimpleNoticeContent> list = simpleNoticeContentService.selectSimpleNoticeContentList(simpleNoticeContent);
        return getDataTable(list);
    }

    @GetMapping("/preview")
    public TableDataInfo preview(SimpleNoticeContent simpleNoticeContent)
    {
        startPage();
        List<SimpleNoticeContent> list = simpleNoticeContentService.selectSimpleNoticeContentList(simpleNoticeContent);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setPicUrl(null);
            list.get(i).setContent(null);
        }
        return getDataTable(list);
    }

    @GetMapping("/preview_for_app")
    public TableDataInfo previewForApp(SimpleNoticeContent simpleNoticeContent)
    {
        startPage();
        List<SimpleNoticeContent> list = simpleNoticeContentService.selectSimpleNoticeContentList(simpleNoticeContent);
        List<SimpleNoticePreview> previewList = new ArrayList<>();
        SimpleNoticeContent c = null;
        for (int i = 0; i < list.size(); i++) {
            c = list.get(i);
            SimpleNoticePreview p = new SimpleNoticePreview();
            p.setNoticeId(c.getNoticeId());
            p.setTitle(c.getTitle());
            p.setSectionId(c.getSectionId());
            p.setTopStatus(c.getTopStatus());
            p.setPicUrl(c.getPicUrl());
            p.setAuthor(c.getAuthor());
            p.setSyncTime(c.getSyncTime());
            previewList.add(p);
        }
        return getDataTable(previewList);
    }

    /**
     * 导出公告列表列表
     */
//    @PreAuthorize("@ss.hasPermi('community:content:export')")
    @Log(title = "公告列表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SimpleNoticeContent simpleNoticeContent)
    {
        List<SimpleNoticeContent> list = simpleNoticeContentService.selectSimpleNoticeContentList(simpleNoticeContent);
        ExcelUtil<SimpleNoticeContent> util = new ExcelUtil<SimpleNoticeContent>(SimpleNoticeContent.class);
        return util.exportExcel(list, "公告列表数据");
    }

    /**
     * 获取公告列表详细信息
     */
//    @PreAuthorize("@ss.hasPermi('community:content:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable("noticeId") String noticeId)
    {
        return AjaxResult.success(simpleNoticeContentService.selectSimpleNoticeContentById(noticeId));
    }

    /**
     * 新增公告列表
     */
//    @PreAuthorize("@ss.hasPermi('community:content:add')")
    @Log(title = "公告列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SimpleNoticeContent simpleNoticeContent)
    {
        return toAjax(simpleNoticeContentService.insertSimpleNoticeContent(simpleNoticeContent));
    }

    /**
     * 修改公告列表
     */
//    @PreAuthorize("@ss.hasPermi('community:content:edit')")
    @Log(title = "公告列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SimpleNoticeContent simpleNoticeContent)
    {
        return toAjax(simpleNoticeContentService.updateSimpleNoticeContent(simpleNoticeContent));
    }

    /**
     * 删除公告列表
     */
//    @PreAuthorize("@ss.hasPermi('community:content:remove')")
    @Log(title = "公告列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable String[] noticeIds)
    {
        return toAjax(simpleNoticeContentService.deleteSimpleNoticeContentByIds(noticeIds));
    }
}
